package app.utils;

import java.lang.reflect.Field;

public class Replace_INTO {
	public static String REPLACE_INTO(Object obj) throws IllegalArgumentException, IllegalAccessException{
		Class<? extends Object> class4obj = obj.getClass();
		Field[] fields = class4obj.getDeclaredFields();
		StringBuilder key=new StringBuilder();
		StringBuilder value=new StringBuilder();
		//创建key列表
		for(Field field:fields){
			field.setAccessible(true);
			String name = field.getName();
			key.append(name).append(',');
		}
		if (key.toString().endsWith(",")) {
			key.deleteCharAt(key.length() - 1);
		}
		//创建值列表
		for(Field field:fields){
			field.setAccessible(true);
			Object object = field.get(obj);
			value.append("'").append(object).append("'").append(',');
		}
		if (value.toString().endsWith(",")) {
			value.deleteCharAt(value.length() - 1);
		}
		
		String sql="replace into "+obj.getClass().getSimpleName().toLowerCase() +" ("+key+") values ("+value+");";
		
		return sql;
	}
}
